import { UserService } from './user.service'
import { Body, Controller, Delete, Get, ParseArrayPipe, Post, Put, Query } from '@nestjs/common'
import { CreateUserDTO, TableQuery, TokenPayload, UpdateUserDTO, UserEntity, UpdateProfileDTO, RequirePermission, UpdateUserPwdDTO } from '@/common'

@Controller('system/user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  /** 新建用户 */
  @RequirePermission(['system:user:create'])
  @Post('create')
  public create(@Body() createDto: CreateUserDTO) {
    return this.userService.create(createDto)
  }

  /** 根据传入 id 组删除单个或者多个数据 */
  @RequirePermission(['system:user:delete'])
  @Delete('delete')
  public delete(@Query('userIds', new ParseArrayPipe()) userIds: string[]) {
    return this.userService.delete(userIds)
  }

  /** 修改用户信息 */
  @RequirePermission(['system:user:update'])
  @Put('update')
  public update(@Body() updateDto: UpdateUserDTO) {
    return this.userService.update(updateDto)
  }

  /** 修改用户个人信息 */
  @Put('updateProfile')
  public updateProfile(@TokenPayload('userId') userId: string, @Body() updateDto: UpdateProfileDTO) {
    return this.userService.updateProfile(userId, updateDto)
  }

  /** 查询用户分页列表 */
  @RequirePermission(['system:user:query'])
  @Get('list')
  public findList(@TableQuery() queryParams: TableQueryParams<UserEntity>) {
    return this.userService.findList(queryParams)
  }

  /** 根据用户 ID 查找用户信息 */
  @RequirePermission(['system:user:query'])
  @Get('detail')
  public findOneById(@Query('userId') userId: string) {
    return this.userService.findOneById(userId)
  }

  /** 修改密码 */
  @Put('updatePassword')
  public updatePassword(@Body() updateDto: UpdateUserPwdDTO) {
    return this.userService.updatePassword(updateDto)
  }

  /** 重置密码 */
  @Put('resetPassword')
  public resetPassword(@Body('userId') userId: string) {
    return this.userService.resetPassword(userId)
  }
}
